<template>
  <div class="app-container">
    <el-card class="query-card">
      <el-form ref="query" :model="page.query" inline label-position="right" label-width="80px">
        <el-form-item label="日期" prop="dateRange">
          <el-date-picker
            v-model="page.query.dateRange"
            type="daterange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            value-format="yyyy-MM-dd"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="班次" prop="shiftId">
          <el-select v-model="page.query.shiftId" clearable style="width: 100%;" placeholder="请选择班次">
            <el-option
              v-for="item in shiftList"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="工序" prop="processId">
          <el-select
            v-model="page.query.processId"
            style="width: 100%"
            placeholder="请选择工序"
            filterable
            clearable
          >
            <el-option
              v-for="item in processOptions"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" @click="handleSearch">查询</el-button>
          <el-button icon="el-icon-refresh" @click="resetQueryForm">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <el-card class="table-card">
<!--      <div>
        <span>分类属性列表</span>
      </div>
      <el-divider />-->
      <div class="table-button">
        <el-button type="primary" class="el-icon-plus" @click="calculateWagesClick">计算工资
        </el-button>
        <el-button type="danger" plain class="el-icon-folder-delete" @click="handleBatchDelete">批量删除</el-button>
        <el-tooltip content="刷新" class="table-right-btn">
          <el-button size="mini" circle icon="el-icon-refresh" @click="doPage" />
        </el-tooltip>
      </div>
      <el-table
        v-loading="tableLoading"
        :data="tableData"
        style="width: 100%"
        border
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="45" />
        <el-table-column label="日期" prop="date" />
        <el-table-column label="工序名称" prop="processName" />
        <el-table-column label="班次" prop="shiftName" />
        <el-table-column label="产量" prop="quantity" />
        <el-table-column label="总计件工资" prop="totalPieceRateWage" />
        <el-table-column label="单人计件工资" prop="pieceRateWage" />
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
              type="text"
              @click="detailClick(scope.row)"
            >详情
            </el-button>
            <el-popconfirm
              confirm-button-text='确定'
              cancel-button-text='取消'
              placement="top"
              style="margin-left: 10px"
              title="是否删除此条数据"
              @confirm="handleDelConfirm(scope.row)"
            >
              <el-button slot="reference" type="text">删除</el-button>
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        :page-sizes="[10, 20, 30, 40]"
        :page-size="10"
        layout="total, sizes, prev, pager, next, jumper"
        :total="page.total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </el-card>
    <div v-if="calculateVisible">
      <calculate-module ref="calculateModule" :visible="calculateVisible" @close="handleClose" />
    </div>
    <div v-if="detailVisible">
      <piece-rate-wage-detail ref="detailModule" :visible="detailVisible" @close="handleClose" />
    </div>
  </div>
</template>

<script>
import { selectGroupPage, deletePieceRateWageGroup } from '@/api/financial/pieceRateWage'
import CalculateModule from '@/views/financial/pieceRateWage/modules/calculateModule.vue'
import { selectAllShift } from '@/api/erp/shift'
import { selectAllProcess } from '@/api/erp/process'
import PieceRateWageDetail from '@/views/financial/pieceRateWage/modules/detailModule.vue'

export default {
  name: 'Index',
  components: { PieceRateWageDetail, CalculateModule },
  data() {
    return {
      tableData: [],
      tableLoading: false,
      selected: [],
      shiftList: [],
      processOptions: [],
      page: {
        page: 1,
        size: 10,
        total: 0,
        query: {
          shiftId: '',
          date: '',
          employeeId: '',
          processId: '',
          dateRange: ['', '']
        }
      },
      calculateVisible: false,
      detailVisible: false
    }
  },
  mounted() {
    this.doPage()
    this.getProcessList()
    this.selectAllShift()
  },
  methods: {
    handleSearch() {
      this.page.page = 1
      this.doPage()
    },
    doPage() {
      // 分页查询
      this.tableLoading = true
      selectGroupPage(this.page).then(resp => {
        if (resp.success) {
          this.tableData = resp.data.records
          this.page.total = resp.data.total
        }
      }).finally(() => {
        this.tableLoading = false
      })
    },
    // 选中事件
    handleSelectionChange(row) {
      this.selected = row
    },
    resetQueryForm() {
      this.$refs.query.resetFields()
      this.page.query.employeeName = ''
      this.page.query.employeeId = ''
      this.$refs.customerQuery.resetQueryForm()
      this.doPage()
    },
    // 计算按钮点击
    calculateWagesClick() {
      this.calculateVisible = true
      this.$nextTick(() => {
        this.$refs.calculateModule.init()
      })
    },
    // 修改按钮点击
    detailClick(row) {
      this.detailVisible = true
      this.$nextTick(() => {
        this.$refs.detailModule.init(row.id)
      })
    },
    // 批量删除点击
    handleBatchDelete() {
      if (this.selected.length === 0) {
        this.$message.warning('请选择一条或多条数据')
        return
      }
      this.$confirm('是否批量删除选中的数据?', '删除提醒', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'error'
      }).then(() => {
        this.tableLoading = true
        const data = { ids: this.selected.map(item => item.id) }
        deletePieceRateWageGroup(data).then(resp => {
          if (resp.success) {
            this.$message.success('删除成功')
          }
        }).finally(() => {
          this.tableLoading = false
          this.doPage()
        })
      })
    },
    // 分页大小
    handleSizeChange(val) {
      this.page.size = val
      this.doPage()
    },
    // 页码
    handleCurrentChange(val) {
      this.page.page = val
      this.doPage()
    },
    // 表单关闭
    handleClose() {
      this.calculateVisible = false
      this.detailVisible = false
      this.doPage()
    },
    handleDelConfirm(row) {
      this.tableLoading = true
      const data = { ids: [row.id] }
      deletePieceRateWageGroup(data).then(resp => {
        if (resp.success) {
          this.$message.success('删除成功')
          this.doPage()
        }
      }).finally(() => {
        this.tableLoading = false
      })
    },
    selectAllShift() {
      this.formLoading = true
      selectAllShift().then(resp => {
        if (resp.success) {
          this.shiftList = resp.data
        }
      }).finally(() => {
        this.formLoading = false
      })
    },
    // 获取工序
    getProcessList() {
      selectAllProcess().then(resp => {
        if (resp.success) {
          this.processOptions = resp.data
        }
      })
    },
    handleChooseEmployee(row) {
      if (row) {
        this.page.query.employeeId = row.id
      }
    }
  }
}
</script>

<style scoped>

</style>
